#include "CGraphicsHandler.h"
#include "DXUT.h"
#include "SDKmisc.h"

CGraphicsHandler::CGraphicsHandler(){
	pSprite=NULL;
	pTextSprite=NULL;
	pFont=NULL;

	vScale=D3DXVECTOR2(1, 1);
	nFrame = 1;
	Color = CLR_WHITE;
	fAngle = 0;

	D3DXMatrixIdentity(&mScale);
	D3DXMatrixIdentity(&mRotation);
	D3DXMatrixIdentity(&mTranslation);
	D3DXMatrixIdentity(&mResize);

};
CGraphicsHandler::~CGraphicsHandler(){
	Release();
}

void CGraphicsHandler::Init(IDirect3DDevice9* pd3dDevice){
	// Initialize the font
    D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
                              OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
                              L"Arial", &pFont );

	D3DXCreateSprite( pd3dDevice, &pTextSprite );
	D3DXCreateSprite( pd3dDevice, &pSprite );

	txtHelper = new CDXUTTextHelper( pFont, pTextSprite, 15 );

	IDirect3DSurface9* pBackBuffer = NULL;
    pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
	D3DSURFACE_DESC d3dSurface;
	pBackBuffer->GetDesc(&d3dSurface);
	D3DXMatrixScaling(&mResize, (float)d3dSurface.Width/1920.0f,(float)d3dSurface.Height/1080.0f,1.0f);

	pBackBuffer->Release();


}
void CGraphicsHandler::Reset(IDirect3DDevice9* pd3dDevice){
	if( pFont )
        pFont->OnResetDevice();

	if( !pTextSprite )
		D3DXCreateSprite( pd3dDevice, &pTextSprite );
	if( !pSprite )
		D3DXCreateSprite( pd3dDevice, &pSprite );
	
	SAFE_DELETE( txtHelper );
	txtHelper = new CDXUTTextHelper( pFont, pTextSprite, 15 );

	IDirect3DSurface9* pBackBuffer = NULL;
    pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
	D3DSURFACE_DESC d3dSurface;
	pBackBuffer->GetDesc(&d3dSurface);
	D3DXMatrixScaling(&mResize, (float)d3dSurface.Width/1920.0f,(float)d3dSurface.Height/1080.0f,1.0f);

	pBackBuffer->Release();
}
void CGraphicsHandler::Lost(){
	if( pFont )
        pFont->OnLostDevice();

	SAFE_RELEASE( pTextSprite );
	SAFE_RELEASE( pSprite );
}
void CGraphicsHandler::Release(){
	SAFE_RELEASE( pTextSprite );
	SAFE_RELEASE( pSprite );
	SAFE_RELEASE( pFont );
	SAFE_DELETE( txtHelper );
}

void CGraphicsHandler::BeginRender(){
	pSprite->Begin( D3DXSPRITE_ALPHABLEND );
	txtHelper->Begin();
}

void CGraphicsHandler::EndRender(){
	txtHelper->End();
	pSprite->End();
}

/*------------------------------------------------------------------------------------------------
-Draw Functions
------------------------------------------------------------------------------------------------*/
/*
void CGraphicsHandler::DrawTileset(CSprite *sprite, int xframe, int yframe, D3DXVECTOR2 pos, D3DXVECTOR2 scale, float angle, D3DCOLOR argb){
	RECT rct;
	 
	if (xframe<1){xframe=1;}
	if (yframe<1){yframe=1;}

	rct.right=(long)sprite->SpriteWidth*xframe;
	rct.left=(long)(sprite->SpriteWidth*xframe)-(long)(sprite->SpriteWidth);
	rct.bottom=(long)(sprite->SpriteHeight*yframe);
	rct.top=(long)(sprite->SpriteHeight*yframe)-(long)(sprite->SpriteHeight);

	angle = min(max(angle, -360), 360);
	angle = D3DXToRadian(angle);
	
	D3DXMatrixScaling(&sprite->scale, scale.x,scale.y,1.0f);
	D3DXMatrixTranslation(&sprite->trans,pos.x,pos.y,0);
	D3DXMatrixRotationZ(&sprite->rot, angle);

	sprite->final = sprite->scale * sprite->rot * sprite->trans;
	pSprite->SetTransform(&sprite->final);
	pSprite->Draw(sprite->texture, &rct, &sprite->SpriteCenter, NULL, argb);
};*/
void CGraphicsHandler::DrawSprite(CSprite *sprite, D3DXVECTOR2 pos){
	int frame=nFrame;
	float angle=fAngle;

	RECT rct;
	rct.left=0;
	rct.top=0;
	rct.bottom=(long)sprite->SpriteHeight;

	if (frame>sprite->numOfFrames){frame=sprite->numOfFrames;}
	if (frame<0){frame=0;}

	if (frame == 0){
		
		rct.right=(long)sprite->SpriteWidth*sprite->AnimationCount;
		rct.left=(long)(sprite->SpriteWidth*sprite->AnimationCount)-(long)(sprite->SpriteWidth);
		if (sprite->AnimationCount <= sprite->numOfFrames){
			if (sprite->AnimationStep < sprite->AnimationCount){
				sprite->AnimationStep++;
			}else{
				sprite->AnimationCount++;
				sprite->AnimationStep=0;
			}
		}
		if (sprite->AnimationCount > sprite->numOfFrames){
			sprite->AnimationCount=1;
		}
		
	}else{
		rct.right=(long)sprite->SpriteWidth*frame;
		rct.left=(long)(sprite->SpriteWidth*frame)-(long)(sprite->SpriteWidth);
	}
	angle = min(max(angle, -360), 360);
	angle = D3DXToRadian(angle);
	
	
	D3DXMatrixTranslation(&mTranslation,pos.x,pos.y,0);

	D3DXMATRIX mFinal = mScale * mRotation * mTranslation * mResize;
	pSprite->SetTransform(&mFinal);
	pSprite->Draw(sprite->GetTexture(), &rct, NULL, NULL, Color);

}

void CGraphicsHandler::DrawText(const WCHAR* txt, D3DXVECTOR2 pos){
    txtHelper->SetInsertionPos( (int)pos.x, (int)pos.y );
    txtHelper->SetForegroundColor( Color );
    txtHelper->DrawTextLine( txt );
};